<template>
	<ve-dialog
		width="750px"
		title="批量修改试题"
		:show.sync="show"
		:on-close="onCloseDialog"
	>
		<div class="log">
			<p class="title"><img class="title-icon" src="@rc/assets/images/icon-basic.png" alt="">基本信息</p>
			<div class="content">
				<select-groups-base
					ref="baseRef"
					:basic-info="basicInfo"
					label-position="right"
					label-width="80px"
					:chapter-multiple="true"
					:knowledge-multiple="true"
					:prop-list="searchProps"
				/>
			</div>
		</div>
		<div class="log">
			<p class="title"><img class="title-icon" src="@rc/assets/images/icon-tag.png" alt="">标签信息</p>
			<div class="content">
				<select-groups-tag
					ref="tagRef"
					label-position="right"
					label-width="80px"
					:area-multiple="true"
				/>
			</div>
		</div>
		<template #footer>
			<el-button @click="onCloseDialog">取 消</el-button>
			<el-button type="primary" :loading="isLoading" @click="onConfirm">确 定</el-button>
		</template>
	</ve-dialog>
</template>
<script>
import SelectGroupsBase from '../../components/select-groups-base/index.vue';
import SelectGroupsTag from '../../components/select-groups-tag/index.vue';
import questionApi  from "@rc/apis/rc/question.mjs";

export default {
    props:{
        ids:{ // 试题ID
            type: Array,
            required:true
        },
        show:{ // 弹窗显示
            type: Boolean,
            default: false
        },
        projectId:{ //项目ID
           type: String,
           required:true
        },
        subjectId:{  //科目ID
           type: String,
           required:true
        }
    },
    components:{
       SelectGroupsBase,
       SelectGroupsTag
    },
    computed:{
        basicInfo() {
            return {
                projectId:[this.projectId],
                subjectId:this.subjectId
            };
        }
    },
    data(){
        return {
            isLoading:false,
            searchProps:["chapter", "knowledgePoint","questionDeveloperId"],
        };
    },
    methods:{
        onCloseDialog() {
            this.$emit('cancel');
        },
        //点击确定按钮
        onConfirm() {
            //处理表单数据
            const baseInfo  = this.$refs.baseRef.api_result();
            const {applicableYear,areaList,labelList}  = this.$refs.tagRef.api_result();
            let formInfo = {
                applicableYear,
                labelList,
                areaList,
                ...baseInfo,
            };
            const {projectId} = formInfo;
            formInfo.projectId = projectId && projectId.length ? projectId[projectId.length-1]:'';
            delete formInfo.chapterIds;
            formInfo.labelList = formInfo.labelList?formInfo.labelList.filter(label=>label.labelDetailId):[];
            delete formInfo.knowledgeName;
            // 去除空字段
            let params = {};
            for(let i in formInfo) {
                const isArray = Array.isArray(formInfo[i]);
                if( isArray&& formInfo[i].length || !isArray && formInfo[i]){
                    params[i] = formInfo[i];
                }
            }
            const keys = Object.keys(params);
            if(keys.length<=2){
                this.$message.warning('您没有修改任何信息');
                return;
            }
            this.isLoading = true;
            questionApi.updateBatchQuestion({
                    data: {
                        resourceIds:this.ids,
                        ...params
                    },
                    success: () => {
                    this.$message.success('试题修改成功');
                    this.$emit('success');
                },
                complete: () => {
                    this.isLoading = false;
                }
            });
        }
    }
};
</script>
<style lang="less" scoped>
    .basic,.tag,.log {
        margin: 0 20px;
        .title{
            height: 40px;
            line-height: 40px;
            display: flex;
            align-items: center;
            font-weight: bold;
            .title-icon{
                width: 20px;
                height: 20px;
                margin-right: 6px;
            }
        }
        .content{
            padding: 10px 10px;
        }
    }
</style>
